<?php
class Kanedo_Session
{
	protected $_namespace;
	protected $_cache = array();
	public function __construct($namespace)
	{
		@session_start();
		$this->_namespace = $namespace;
	}
	public function __set($key, $val)
	{
		$_SESSION[$this->_namespace][$key] = $val;
	}
	
	public function __get($key)
	{
		if (isset($_SESSION[$this->_namespace][$key]))
		{
			return $_SESSION[$this->_namespace][$key];
		}else {
			return false;
		}
	}
	
	public function __unset($key)
	{
		unset($_SESSION[$this->_namespace][$key]);
		if (isset($_COOKIE[session_name()]))
    	setcookie(session_name(), '', time() - 42000, '/');
		@session_destroy();
	}
	
	public function getNamespace()
	{
		return $this->_namespace;
	}
	
	public function __sleep()
	{
		$session = $_SESSION[$this->_namespace];
		foreach ($session as $key => $val)
		{
			$this->_cache[$key] = $val;
		}
		return array("_namespace", "_cache");
	}
	
	public function __wakeup()
	{
		foreach($this->_cache as $key => $val)
		{
			$_SESSION[$this->_namespace][$key] = $val;
		}
	}
	
	public function sessionDestroy()
	{
		foreach($_SESSION as $key => $val)
		{
			unset($_SESSION[$key]);
		}
		session_destroy();
	}
}
?>